﻿using System;
using System.Collections.Generic;
using System.Text;
using FingerPrintRecognizer.Common;
using MySql.Data.MySqlClient;

namespace FingerPrintRecognizer.DL
{
    public class FingerprintDAO : IDataProvider
    {
        const string desc = "This Plugin is a implementation of IDataProvider interface.";
        const string name = "MySQL Plugin";
        const string author = "Md. Saidul Islam";
        const string website = "www.duniya-nai.com";
        const int majorVersion = 0;
        const int minorVersion = 4;

        private MySqlConnection conn;

        public FingerprintDAO() {
            this.conn = getConnection("localhost", "3306", "fingerprintdb", "root", "");
        }

        private MySqlConnection getConnection(string server, string port, string database,string user, string password)
        {
            string MyConString = String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};", server, database, user, password);
            MySqlConnection connection = new MySqlConnection(MyConString);
            return connection;
        }
        #region IDataProvider Members

        public Person getPersonByID(int id)
        {
            throw new NotImplementedException();
        }

        public Person getPersonByNationalID(string id)
        {
            Person p = null;
            this.conn.Open();
            MySqlCommand cmd = this.conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM persons WHERE NationalID = @nationalid";
            cmd.Parameters.AddWithValue("@nationalid", id);
            MySqlDataReader rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
                p = new Person();
                p.PersonID = rdr.GetInt32("PersonID");
                p.NationalID = rdr.GetString("NationalID");
                p.Title = rdr.GetString("Title");
                p.FirstName = rdr.GetString("FirstName");
                p.LastName = rdr.GetString("LastName");
	            p.DateOfBirth = rdr.GetDateTime("DateOfBirth");
                p.Address = rdr.GetString("Address");
                p.City = rdr.GetString("City");
                p.Country = rdr.GetString("Country");
            }
            this.conn.Close();

            return p;
        }

        public User getUserByLoginID(string loginID)
        {
            User usr = null;
            this.conn.Open();
            MySqlCommand cmd = this.conn.CreateCommand();
            cmd.CommandText = "SELECT p.*, u.LoginID,u.Password,u.UserType FROM users as u,persons as p WHERE u.PersonID = p.PersonID AND u.LoginID = @loginID";
            cmd.Parameters.AddWithValue("@loginID", loginID);
            MySqlDataReader rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
                usr = new User();

                usr.LoginID = loginID;
                usr.LoginPass = rdr.GetString("Password");
                usr.Type = (User.UserType)rdr.GetInt32("UserType");

                usr.PersonID = rdr.GetInt32("PersonID");
                usr.NationalID = rdr.GetString("NationalID");
                usr.Title = rdr.GetString("Title");
                usr.FirstName = rdr.GetString("FirstName");
                usr.LastName = rdr.GetString("LastName");
                usr.DateOfBirth = rdr.GetDateTime("DateOfBirth");
                usr.Address = rdr.GetString("Address");
                usr.City = rdr.GetString("City");
                usr.Country = rdr.GetString("Country");
            }
            this.conn.Close();

            return usr;
        }

        public bool beginFPSearch()
        {
            throw new NotImplementedException();
        }

        public byte[][] getNextRow()
        {
            throw new NotImplementedException();
        }

        public bool endFPSearch()
        {
            throw new NotImplementedException();
        }

        public void savePerson(Person person) {
            this.conn.Open();
            MySqlCommand cmd =  this.conn.CreateCommand();
            cmd.CommandText = "INSERT INTO persons VALUES(NULL, @nationalid, @title, @fname, @lname, @dob, @address, @city, @country, @image)";
            
            cmd.Parameters.AddWithValue("@nationalid" , person.NationalID);
            cmd.Parameters.AddWithValue("@title", person.Title);
            cmd.Parameters.AddWithValue("@fname", person.FirstName);
            cmd.Parameters.AddWithValue("@lname", person.LastName);
            cmd.Parameters.AddWithValue("@dob", person.DateOfBirth);
            cmd.Parameters.AddWithValue("@address", person.Address);
            cmd.Parameters.AddWithValue("@city", person.City);
            cmd.Parameters.AddWithValue("@country", person.Country);
            cmd.Parameters.AddWithValue("@image", null);

            cmd.ExecuteNonQuery();
            this.conn.Close();


        }
        #endregion

        #region IPlugin Members

        public void pluginLoaded()
        {
            throw new NotImplementedException();
        }

        public PluginInfo getPluginInfo()
        {
            PluginInfo pi = new PluginInfo { 
                PluginName = name, 
                PluginAuthor = author,
                Website = website,
                Description = desc,
                Type = PluginInfo.PluginType.DataLayer,
                Version = String.Format("{0}.{1}",majorVersion,minorVersion)
            };
            return pi;
        }

        #endregion
    }
}
